home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / pvquan16.zip / QUANT / QUANT.C < prev    next >
C/C++ Source or Header  |  1992-11-30  |  8KB  |  226 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *                  Copyright (c) 1991, Frank van der Hulst             *
  4.  *                          All Rights Reserved                         *
  5.  *                                                                      *
  6.  * Authors:                                                             *
  7.  *        FvdH - Frank van der Hulst (Wellington, NZ)                   *
  8.  *                                                                      *
  9.  * Versions:                                                            *
  10.  *    V1.1 910626 FvdH - QUANT released for DBW_RENDER                  *
  11.  *    V1.2 911021 FvdH - QUANT released for PoV Ray                     *
  12.  *    V1.3 911031 FvdH - Added 320x200x256x4 pages support              *
  13.  *    V1.4 920303 FvdH - Ported to GNU                                  *
  14.  *                     - Changed usage() to fix bug                     *
  15.  *    V1.5 920331 FvdH - Read Targa file format                         *
  16.  *         920403 FvdH - Allow any number of files                      *
  17.  *    V1.6 921023 FvdH - Produce multi-image GIFs                       *
  18.  *                     - Port to OS/2 IBM C Set/2                       *
  19.  *                                                                      *
  20.  ************************************************************************/
  21. /*
  22.  * quant.c
  23.  *
  24.  * Program to do colour quantization. This program reads PoV Ray raw
  25.  * format files and quantizes the image(s) to produce a file in one of
  26.  * 3 formats -- 2D, 3D or GIF. It can use either Heckbert's median-splitting
  27.  * algorithm, or else the Octree Quantisation algorithm by Michael Gervautz
  28.  * and Werner Purgathofer.
  29.  *
  30.  * This program compiles using Turbo-C v2.01 and runs on an MS-DOS machine,
  31.  * using cc on a SCO Unix system, gcc on a Sun system, IBM's icc on OS/2.
  32.  */
  33.  
  34. #include <string.h>
  35. #ifdef __TURBOC__
  36. #include <ctype.h>
  37. #endif
  38.  
  39. #include "quant.h"
  40.  
  41. #ifdef HECKBERT
  42. #include "heckbert.h"
  43. #else
  44. #include "octree.h"
  45. #endif
  46.  
  47. unsigned long HUGE_PTR Histogram;        /* image histogram */
  48. unsigned char HUGE_PTR RGBmap;            /* RGB -> index map */
  49.  
  50. unsigned int    ColormaxI;                /* # of colors, 2^input_bits */
  51. UCHAR palette[MAXCOLORS][3];
  52.  
  53. #ifdef __TURBOC__
  54. char                disp_image    = 1;        /* Display image while quantising */
  55. #else
  56. char                disp_image    = 0;        /* Display image while quantising */
  57. #endif
  58. char                 input_type  = 1;        /* Input format switch */
  59.  
  60. static int output_bits        = 6;        /* No. of sig. bits on output (must be <= 8) */
  61. #ifdef HECKBERT
  62. static int fast_quant        = 0;        /* Quantisation speed switch */
  63. /*    If zero, the rgbmap will be built very slowly, but more accurately. The
  64.     error introduced by the approximation is usually small.
  65.  
  66.     If one, the rgbmap will be constructed quickly. */
  67. #endif
  68.  
  69. static int image_width        = 320;    /* Width of image in pixels */
  70. static int image_height        = 200;    /* Height of image in pixels */
  71. static int output_colours     = 256;    /* No. of separate colours to produce ( <= 256) */
  72. static int output_type     = 1;     /* Output format switch */
  73.  
  74. char *input_file;
  75. int num_files = 1;
  76.  
  77. void err_exit(void)
  78. {
  79. #ifdef __OS2__
  80.         getchar();
  81. #endif
  82.         exit(1);
  83. }
  84.  
  85. void usage(char *prog_name)
  86. {
  87.     printf("Command syntax: %s [-O=outputbits][-C=colours][-S=speed][-T=outtype]\n", prog_name);
  88.     printf("                   [-D=display][-W=width][-H=height][-I=intype][-N=numfiles] filename\n\n");
  89.     printf("outputbits = bits per colour being output          [6]\n");
  90. #ifdef HECKBERT
  91.     printf("speed      = 0 or 1                                [%d]\n", fast_quant);
  92. #endif
  93.     printf("outtype    = 0 for 4-plane, 1 for planar,2 for GIF [%d]\n", output_type);
  94.     printf("intype     = 0 for raw, 1 for Targa                [%d]\n", input_type);
  95. #ifdef __TURBOC__
  96.     printf("display    = 1 or 0 for displaying during output   [%d]\n", disp_image);
  97. #endif
  98.     printf("colours    = number of separate colours to produce [%d]\n", output_colours);
  99.     printf("width      = width of image in pixels              [%d]\n", image_width);
  100.     printf("height     = height of image in pixels             [%d]\n", image_height);
  101.     printf("numfiles   = number of files to quantise           [%d]\n", num_files);
  102.     printf("filename   = name part of file(s) to read, without extension\n");
  103.     err_exit();
  104. }
  105.  
  106. /********************************************************************
  107.  Process command line arguments. */
  108.  
  109. void get_args(int argc, char *argv[])
  110. {
  111.     int i;
  112.  
  113.     if (argc == 1) usage(argv[0]);
  114.  
  115.     for (i = 1; i < argc; i++) {
  116.         if (argv[i][0] != '/' && argv[i][0] != '-') {
  117.             input_file = argv[i];
  118.             continue;
  119.         }
  120.  
  121.         if (argv[i][2] != '=') {
  122.             printf("Invalid command line switch: %s\n", argv[i]);
  123.             usage(argv[0]);
  124.         }
  125.  
  126.         switch(toupper(argv[i][1])) {
  127.         case 'O': output_bits        = atoi(&argv[i][3]); break;
  128. #ifdef HECKBERT
  129.         case 'S': fast_quant            = atoi(&argv[i][3]); break;
  130. #endif
  131.         case 'T': output_type        = atoi(&argv[i][3]); break;
  132.         case 'I': input_type            = atoi(&argv[i][3]); break;
  133. #ifdef __TURBOC__
  134.         case 'D': disp_image           = atoi(&argv[i][3]); break;
  135. #endif
  136.         case 'C': output_colours     = atoi(&argv[i][3]); break;
  137.         case 'W': image_width        = atoi(&argv[i][3]); break;
  138.         case 'H': image_height     = atoi(&argv[i][3]); break;
  139.         case 'N': num_files        = atoi(&argv[i][3]);    break;
  140.         default:
  141.             printf("Invalid command line switch: %s\n", argv[i]);
  142.             usage(argv[0]);
  143.         }
  144.     }
  145. }
  146.  
  147. void main(int argc, char *argv[])
  148. {
  149.     int colors, i;
  150. #ifdef HECKBERT
  151.     long int li;
  152. #endif
  153.  
  154.     printf("QUANT v1.60 -- Colour quantisation for PVRay\n");
  155. #ifdef HECKBERT
  156.     printf("By F van der Hulst, based on COLORQUANT by Craig E. Kolb.\n\n");
  157. #else
  158.     printf("By F van der Hulst, based on Octree Quantisation by Wolfgang Stuerzlinger.\n\n");
  159. #endif
  160.  
  161.     get_args(argc, argv);
  162.         if ((output_type == 0) &&
  163.          ((image_width != 320) ||
  164.           ((image_height != 400) && (image_height != 200)))) {
  165.         printf("Only 320x400 and 320x200 images can be written in 4-plane format.\n");
  166.         err_exit();
  167.     }
  168.  
  169. #ifdef HECKBERT
  170.     CHECK_ALLOC(RGBmap, unsigned char, BYTE_COUNT, "RGB map");
  171.  
  172.     CHECK_ALLOC(Histogram, unsigned long, BYTE_COUNT, "Histogram");
  173.     for (li = 0; li < BYTE_COUNT; li++)        Histogram[li] = 0L;
  174.     open_box_file(output_colours);
  175.  
  176. /* The projected frequency arrays of the largest box are zeroed out as
  177.     as part of open_box_file(). */
  178.  
  179. #endif
  180.  
  181.     for (i = 0; i < num_files; i++) {
  182.         open_file(input_file, i);
  183.  
  184. #ifdef HECKBERT
  185.         printf("Building Histogram from %s_%d: ...", input_file, i);
  186.         QuantHistogram(get_box(0));
  187.         printf("\b\b\bDone\n");
  188. #else
  189.         printf("Building Octree from %s_%d: ...", input_file, i);
  190.         generateoctree();            /* read the file through */
  191.  
  192.         printf("\b\b\bDone\n");
  193. #endif
  194.         close_file();
  195.     }
  196.  
  197.     for (i = 0; i < MAXCOLORS; i++)          /* init palette */
  198.  
  199.         palette[i][0] = palette[i][1] = palette[i][2] = 0;    /* 0 usually is black ! */
  200.  
  201. #ifdef HECKBERT
  202.     colors = colorquant(output_colours, INPUT_BITS, fast_quant,
  203.                         (double) ((1 << output_bits) - 1) / ((1 << INPUT_BITS) - 1));
  204.  
  205.     close_box_file();
  206. #ifdef __TURBOC__
  207.     free((void far *)Histogram);
  208. #else
  209.     free(Histogram);
  210. #endif
  211. #else
  212.     colors = calc_palette(1, (double) ((1 << output_bits) - 1) / 0xff);
  213.                         /* entry 0 is left black here ! */
  214.  
  215. #endif
  216.     printf("%d %s quantized to %d colors.\n", num_files, num_files == 1 ? "file" : "files", colors);
  217.     write_file(num_files, input_file, image_width, image_height, colors, output_type);
  218. #ifdef HECKBERT
  219. #ifdef __TURBOC__
  220.     free((void far *)RGBmap);
  221. #else
  222.     free(RGBmap);
  223. #endif
  224. #endif
  225. }
  226.